package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.sys.entity.NotifyParm;
import com.dataseek.iot.sys.service.DbService;
import com.dataseek.iot.sys.service.NotifyParmService;
import com.dataseek.iot.sys.vo.NotifyParmVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/notifyparm")
public class NotifyParmController {

    @Autowired
    private NotifyParmService notifyParmService;

    @Autowired
    private DbService dbService;

    /**
     * 根据编码code 和 状态查询
     * @param page
     * @param size
     * @param parmCode
     * @param parmName
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult query(@RequestParam(value="page")  Integer page,
                                      @RequestParam(value="size")  Integer size,
                                      @RequestParam(value="parmCode", required=false) String parmCode,
                                      @RequestParam(value="parmName", required=false)  String parmName) throws IOException {
        NotifyParmVo notifyParmVo = notifyParmService.queryAction(parmCode,parmName,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",notifyParmVo);
    }

    /**
     * 新增通知参数
     * @param notifyParm
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insert(@RequestBody NotifyParm notifyParm) throws IOException {
        Integer code  = notifyParmService.insertAction(notifyParm);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"通知编码重复，请重新输入!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }

    /***
     * 更新通知参数
     * @param notifyParm
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(@RequestBody NotifyParm notifyParm) throws IOException {
        Integer code  = notifyParmService.updateAction(notifyParm);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult delete(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Integer> listIds = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = notifyParmService.deleteAction(listIds);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

}
